﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

namespace BDpic
{
    class Program
    {

        private static string url = @"http://image.baidu.com/channel/listjson";//?fr=channel&tag1=美女&tag2=全部&sorttype=0&pn=30&rn=60&ie=utf8&oe=utf-8";
        private static string downPath = Environment.CurrentDirectory + "\\download\\";
        private static List<string> imgQueue = new List<string>();//存入待下载的img地址

        //pn=0&rn=24&tag1=壁纸&tag2=名车&tag3=全部&width=1600&height=900&ic=0&ie=utf8&oe=utf-8

        private static int pn = 0;                  //何处开始
        private static int rn = 5000;                 //数量
        private static string tag1 = "美女";        //关键字1
        private static string tag2 = "全部";        //关键字2
        private static string tag3 = "";            //关键字3
        private static int width = 1600;            //图片宽度
        private static int height = 900;            //图片高度
        private static int ic = 0;                  //颜色枚举 
        private static string ie = "utf8";          //...
        private static string oe = "utf-8";         //...


        static void Main(string[] args)
        {
            url += "?fr=channel&tag1=" + tag1;
            url += "&pn=" + pn;
            url += "&rn=" + rn;
            if (tag2 != "") url += "&tag2=" + tag2;
            if (tag3 != "") url += "&tag3=" + tag3;
            if (width != 0) url += "&width=" + width;
            if (height != 0) url += "&height=" + height;
            if (ic != 0) url += "&ic=" + ic;
            url += "&ie=" + ie;
            url += "&oe=" + oe;


            url = UrlEncode(url);

            WebClient wc = new WebClient();
            ServicePointManager.Expect100Continue = false;
            wc.Encoding = Encoding.UTF8;
            wc.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
            wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0");
            wc.Headers.Add("X-Requested-With", "XMLHttpRequest");
            wc.Headers.Add("Content-Type", "application/json");
            string responseData = wc.DownloadString(url);
            AnanysiString(responseData);
            string strtr = "";
            foreach (var item in imgQueue)
            {
                strtr += item + "\r\n";
            }
            File.WriteAllText("C:\\9.txt", strtr);
        }


        //解析json
        private static void AnanysiString(string json)
        {
            //string json = File.ReadAllText("C:\\1.txt", Encoding.Default);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            jss.MaxJsonLength = Int32.MaxValue;
            Dictionary<string, object> dic = jss.Deserialize<Dictionary<string, object>>(json);
            System.Collections.ArrayList al = (System.Collections.ArrayList)dic["data"];
            foreach (Dictionary<string, object> item in al)
            {
                if (item.Count > 0)
                    imgQueue.Add(item["download_url"].ToString());
            }
        }

        //UrlEncode
        private static string UrlEncode(string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = Encoding.UTF8.GetBytes(str);
            for (int i = 0; i < byStr.Length; i++)
            {
                if (byStr[i] > 128)
                    sb.Append(@"%" + Convert.ToString(byStr[i], 16));
                else
                    sb.Append(Convert.ToChar(byStr[i]));
            }
            return (sb.ToString());
        }
    }
}
